/*
 * @(#)Initializer.java
 * 
 * Copyright(C) 2004 Yujin Kim
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.netexplode.jtunes.client.player;

import java.net.InetAddress;

import javax.jmdns.JmDNS;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.netexplode.jtunes.Constants;
import com.netexplode.jtunes.core.Configuration;
import com.netexplode.jtunes.core.ConfigurationAware;
import com.netexplode.jtunes.model.player.PlayerManager;
import com.netexplode.jtunes.protocol.jmdns.DaapListener;

/**
 * <code>Initializer</code> represents .....
 * 
 * @author ykim
 * @version $Revision: 1.4 $
 * @since 0.1
 */
public class Initializer implements ConfigurationAware {

	private static final Log log = LogFactory.getLog(Initializer.class);

	private Configuration configuration;

	private PlayerManager playerManager;
	
	private DaapListener daapListener; 

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	public void setPlayerManager(PlayerManager playerManager) {
		this.playerManager = playerManager;
	}
	
	public void setDaapListener(DaapListener daapListener) {
		this.daapListener = daapListener;
	}

	public final void init() {

		// search for shared itunes
		if (configuration.isSearchShared()) {
			try {
				final InetAddress inet = InetAddress.getLocalHost();
				final JmDNS jmdns = new JmDNS(inet);
				jmdns.addServiceListener(Constants.DAAP_SERVICE, daapListener);
			}
			catch (Exception e) {
				log.warn(e, e);
			}
		}

		// initialize the player
		try {
			playerManager.init();
		}
		catch (Exception e) {
			log.fatal(e, e);
			System.exit(1);
		}
	}
}